/**
 * Author: luoqi
 * Created Date: 2025-10-09 16:10:25
 * Last Modified: 2025-11-26 23:38:59
 * Modified By: luoqi at <**@****>
 * Copyright (c) 2025 <*****>
 * Description:
 */

#pragma once

#include <streambuf>
#include <iostream>

class StreamAutoCRLF : public std::streambuf {
public:
    StreamAutoCRLF() 
    {
        orgbuf = std::cout.rdbuf();  // save original buffer
        std::cout.rdbuf(this);       // set current object as cout's new buffer
    }

    ~StreamAutoCRLF() 
    {
        std::cout.rdbuf(orgbuf);     // restore original buffer
    }

    virtual int_type overflow(int_type c) override
    {
        if(c != traits_type::eof()) {
            if(c == '\n') {
                if(orgbuf->sputc('\r') == traits_type::eof())  // output \r
                    return traits_type::eof();
            }
            return orgbuf->sputc(c);  // output c
        }
        return traits_type::not_eof(c);
    }
    
private:
    std::streambuf *orgbuf;  // save original buffer
};
